package com.ruoyi.whzj.controller;

import java.util.List;

import com.ruoyi.common.utils.EmptyUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.whzj.domain.WhAutocommont;
import com.ruoyi.whzj.service.IWhAutocommontService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 自定义评论Controller
 * 
 * @author ruoyi
 * @date 2020-11-30
 */
@Controller
@RequestMapping("/whzj/autocommont")
public class WhAutocommontController extends BaseController
{
    private String prefix = "whzj/autocommont";

    @Autowired
    private IWhAutocommontService whAutocommontService;

    @RequiresPermissions("whzj:autocommont:view")
    @GetMapping()
    public String autocommont()
    {
        return prefix + "/autocommont";
    }

    /**
     * 查询自定义评论树列表
     */
    @RequiresPermissions("whzj:autocommont:list")
    @PostMapping("/list")
    @ResponseBody
    public List<WhAutocommont> list(WhAutocommont whAutocommont)
    {
        List<WhAutocommont> list = whAutocommontService.selectWhAutocommontList(whAutocommont);
        return list;
    }

    /**
     * 导出自定义评论列表
     */
    @RequiresPermissions("whzj:autocommont:export")
    @Log(title = "自定义评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WhAutocommont whAutocommont)
    {
        List<WhAutocommont> list = whAutocommontService.selectWhAutocommontList(whAutocommont);
        ExcelUtil<WhAutocommont> util = new ExcelUtil<WhAutocommont>(WhAutocommont.class);
        return util.exportExcel(list, "autocommont");
    }

    /**
     * 新增自定义评论
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("whAutocommont", whAutocommontService.selectWhAutocommontById(id.toString()));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存自定义评论
     */
    @RequiresPermissions("whzj:autocommont:add")
    @Log(title = "自定义评论", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WhAutocommont whAutocommont)
    {
        if(EmptyUtils.isEmpty(whAutocommont.getPid())){
            whAutocommont.setPid("0");
            whAutocommont.setCommontLevel("一级");
        }else{
            whAutocommont.setCommontLevel("二级");
        }
        return toAjax(whAutocommontService.insertWhAutocommont(whAutocommont));
    }

    /**
     * 修改自定义评论
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        WhAutocommont whAutocommont = whAutocommontService.selectWhAutocommontById(id);
        mmap.put("whAutocommont", whAutocommont);
        return prefix + "/edit";
    }

    /**
     * 修改保存自定义评论
     */
    @RequiresPermissions("whzj:autocommont:edit")
    @Log(title = "自定义评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WhAutocommont whAutocommont)
    {
        if(EmptyUtils.isEmpty(whAutocommont.getPid())){
            whAutocommont.setPid("0");
            whAutocommont.setCommontLevel("一级");
        }else{
            whAutocommont.setCommontLevel("二级");
        }
        return toAjax(whAutocommontService.updateWhAutocommont(whAutocommont));
    }

    /**
     * 删除
     */
    @RequiresPermissions("whzj:autocommont:remove")
    @Log(title = "自定义评论", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id)
    {
        return toAjax(whAutocommontService.deleteWhAutocommontById(id));
    }

    /**
     * 选择自定义评论树
     */
    @GetMapping(value = { "/selectAutocommontTree/{id}", "/selectAutocommontTree/" })
    public String selectAutocommontTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("whAutocommont", whAutocommontService.selectWhAutocommontById(id.toString()));
        }
        return prefix + "/tree";
    }

    /**
     * 加载自定义评论树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = whAutocommontService.selectWhAutocommontTree();
        return ztrees;
    }
}
